//
//  B_ActivityAignUpVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/10/16.
//  Copyright © 2018 薛小刚. All rights reserved.
//

#import "B_ActivityAignUpVC.h"
#import "B_ReleaseFooterView.h"
#import "B_MineHeaderMoreView.h"
#import "B_ActivityFirstTVC.h"
#import "B_ChangeDataTVC.h"
#import "B_ActivitySecondTVC.h"
#import "B_ActivityAsignModel.h"

@interface B_ActivityAignUpVC ()<UITextFieldDelegate>

@property (nonatomic, strong) B_ActivityAsignModel * model;

@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_ReleaseFooterView * submitFooter;

@property (nonatomic, strong) NSArray * imageArray;

@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation B_ActivityAignUpVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"活动报名"];
    _selectIndex = 0;
    _imageArray = @[@{@"image":@"B_activity_signup_wechat", @"title":@"微信支付"},
                    @{@"image":@"B_activity_signup_alipay", @"title":@"支付宝支付"},
                    @{@"image":@"B_activity_signup_balance", @"title":@"账户余额支付"}];
    NSDictionary * dic0 = @{@"title":@"姓名", @"placeHolder":@"请输入姓名", @"map_word":@"姓名", @"value":self.model.B_name};
    NSDictionary * dic1 = @{@"title":@"手机号", @"placeHolder":@"请输入手机号", @"map_word":@"手机号", @"value":self.model.B_phone};
    self.dataArray = [NSMutableArray arrayWithObjects:dic0,
                      dic1,
                      nil];
    self.view = self.mainView;
    [self.mainView addSubview:self.submitFooter];
    CGFloat bottomHeight = getTabHeight() + 20;
    [self.submitFooter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainView.mas_left);
        make.right.equalTo(self.mainView.mas_right);
        make.bottom.equalTo(self.mainView.mas_bottom);
        make.height.equalTo(@(bottomHeight));
    }];
    [_mainView setTableViewFrameWithTop:0 left:0 right:0 bottom:-bottomHeight];
    
}

- (B_ActivityAsignModel *)model {
    if (!_model) {
        _model = [[B_ActivityAsignModel alloc] init];
    }
    return _model;
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_ActivityFirstTVC" bundle:nil] forCellReuseIdentifier:@"B_ActivityFirstTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_ChangeDataTVC" bundle:nil] forCellReuseIdentifier:@"B_ChangeDataTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_ActivitySecondTVC" bundle:nil] forCellReuseIdentifier:@"B_ActivitySecondTVC"];
        //TODO:组数
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 3;
        };
        //TODO:cell数
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 1;
            } else if (section == 1) {
                return 2;
            } else if (section == 2) {
                return 3;
            }
            return 0;
        };
        //TODO:组头的高度
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            return 44;
        };
        //TODO:组头
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            B_MineHeaderMoreView * header = [B_MineHeaderMoreView initB_MineHeaderMoreView];
            header.moreButton.hidden = YES;
            header.backgroundColor = [UIColor color_content_background_color];
            NSArray * titleArr = @[@"活动信息", @"报名信息", @"支付方式"];
            header.B_titleLabel.text = titleArr[section];
            return header;
        };
        //TODO:cell的高度
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                return 100;
            } else if (indexPath.section == 1) {
                return 50;
            } else if (indexPath.section == 2) {
                return 50;
            }
            return 0;
        };
        //TODO:cell
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                B_ActivityFirstTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ActivityFirstTVC" forIndexPath:indexPath];
                
                cell.B_titleLabel.text = [NSString stringWithFormat:@"【%@】", weSelf.detailModel.activityTitle];
                cell.B_priceLabel.text = [NSString stringWithFormat:@"¥%@", safeValue1(weSelf.detailModel.applyPrice)];
                
                return cell;
            } else if (indexPath.section == 1) {
                NSDictionary * dictionary = weSelf.dataArray[indexPath.row];
                B_ChangeDataTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ChangeDataTVC" forIndexPath:indexPath];
                
                cell.B_rightImageView.hidden = YES;
                cell.B_keyLabel.text = dictionary[@"title"];
                cell.B_textfield.placeholder = dictionary[@"placeHolder"];
                cell.B_textfield.text = dictionary[@"value"];
                cell.B_textfield.delegate = weSelf;
                cell.B_textfield.tag = indexPath.row;
                return cell;
            } else {
                NSDictionary * dictionary = weSelf.imageArray[indexPath.row];
                B_ActivitySecondTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ActivitySecondTVC" forIndexPath:indexPath];
                cell.B_imageView.image = [UIImage imageNamed:dictionary[@"image"]];
                cell.B_titleLabel.text = dictionary[@"title"];
                if (indexPath.row == weSelf.selectIndex) {
                    cell.B_selectedButton.selected = YES;
                } else {
                    cell.B_selectedButton.selected = NO;
                }
                return cell;
            }
        };
        
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 2) {
                weSelf.selectIndex = indexPath.row;
                [tableView reloadData];
            }
        };
        
        
        
        
        
    }
    return _mainView;
}


- (B_ReleaseFooterView *)submitFooter {
    if (!_submitFooter) {
        WeakSelf(weSelf, self);
        _submitFooter = [B_ReleaseFooterView initB_ReleaseFooterView];
        [_submitFooter.B_SureButton setTitle:@"确认报名" forState:UIControlStateNormal];
        _submitFooter.B_SureButton.backgroundColor = [UIColor color_pink];
        _submitFooter.sureActionBlock = ^{
            ///TODO:执行确认报名的方法
            [weSelf submitData];
        };
    }
    return _submitFooter;
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == 0) {
        self.model.B_name = textField.text;
    } else if (textField.tag == 1) {
//        if ([textField.text isMobileNumber]) {
            self.model.B_phone = textField.text;
//        } else {
//            [KeyWindow makeToast:@"请输入有效的手机号码!"];
//            textField.text = @"";
//        }
    }
}


- (void) submitData {
    if (ISNULL(self.model.B_name)) {
        [KeyWindow makeToast:@"请输入姓名!"];
        return;
    }
    if (ISNULL(self.model.B_phone)) {
        [KeyWindow makeToast:@"请输入手机号码!"];
        return;
    } else if ([self.model.B_phone isMobileNumber]) {

    } else {
        [KeyWindow makeToast:@"请输入有效的手机号码!"];
        return;
    }
    
    NSString * urlString = [NSString stringWithFormat:@"/yearnfor/inter/activity/userActivityApply/%@/%@/%@/%@", self.detailModel.id, getUserId(), self.model.B_name, self.model.B_phone];
    
    [NetworkingManager getNetworkRequestWithUrlString:urlString parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            ///TODO:去支付
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常,请稍后重试!"];
    }];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
